
if ($GLOBAL:LogLevel -eq $null)
{
  $GLOBAL:LogLevel = @{Info=0;Verbose=1;Debug=2}
  $GLOBAL:LogScreenLevel = 'Info'
  $GLOBAL:LogFileLevel = 'Verbose'
  $GLOBAL:LogFileName = $null
  $GLOBAl:LogXML = $FALSE
}

function Log-InititialzieFile($filename, [switch]$Overwrite, [switch]$force)
{
  if (($LogFileName -ne $null) -and (-not $force))
  {
    Log-Warning "Log File already open at $LogFileName"
  }
  else
  {
    if ((Split-Path $filename) -eq "")
    {
      $filename = Join-Path (Get-Location).Path $filename
    }
    
    $filename = [system.io.fileinfo]$filename
    
    $GLOBAL:LogFileName = $filename.FullName
    if ([system.IO.File]::Exists($LogFileName) -and $Overwrite)
    {
      Remove-Item $LogFileName
    }
    
    Log-Message "Log Started $LogFileName"
    $LogFileName
  }
}

function Log-StopFile
{
  $GLOBAL:LogFileName=$null
}



function Log-ToFile($msg,$Kind,$Level,[switch]$NoNewLine)
{
  if( $LogFileName -ne $null -and $LogLevel[$Level] -le $LogLevel[$LogFileLevel])
  {
    $newLineAttr=''
    if($NoNewLine)
    {
      $newLineAttr=" NoNewLine='True'"
    }
    if ($LogXML)
    {
      "<{0} Level='{1}'{2}>{3}</{0}>" -f  $Kind,$Level,$newLineAttr,$msg | Out-File $LogFileName -Append
    }
    else
    {
      "{3:MM/dd/yy hh:mm:ss.fff tt} [{0}/{1}] - {2}" -f  $Kind,$Level,$msg,[datetime]::Now | Out-File $LogFileName -Append
    }
  }
}

function Log-Message ([string]$msg, $Level='Info', $Kind='Message', $ForegroundColor, $BackgroundColor, [switch]$NoNewLine)
{
  begin
  {
    $lines = @()
    if ($msg -ne '')
    {
      $lines += $msg.Split([System.Environment]::NewLine)
    }

  }
  process
  {
    if ($_)
    {
      if ($NoNewLine -and $lines.Count -gt 0)
      {
        $lines[0] = $lines[0] + [string]$_
      }
      else
      {
        $lines += ([string]$_).Split([System.Environment]::NewLine)
      }
    }
  }
  end
  {
    $forground = $NULL
    $background = $NULL
    $nonewlinestr = $null
    
    if ($ForegroundColor -ne $null)
    {
      $forground = "-ForegroundColor $ForegroundColor "
    }
    if ($BackgroundColor -ne $null)
    {
      $background = "-BackgroundColor $BackgroundColor "
    }
    if ($NonewLine)
    {
      $nonewlinestr = "-NoNewline"
    }
    
    foreach ($line in $lines)
    {
      if ($Kind -ne 'Message')
      {
        $outLine = "$($Kind): $line"
      }
      else
      {
        $outLine = $line
      }
      if ($LogLevel[$Level] -le $LogLevel[$LogScreenLevel])
      {
        $expr = '$outLine | Write-Host {0} {1} {2}' -f $forground, $background, $nonewlinestr
        $ExecutionContext.InvokeCommand.InvokeScript($expr) 
      }
      Log-ToFile $line $Kind $Level -NoNewLine:$NoNewLine  
    }
  }
}

function Log-Warning ([string]$msg, $Level='Info', $Kind='Warning', $ForegroundColor='Yellow', $BackgroundColor='Black', [switch]$NonewLine)
{
  begin
  {
    $lines = @()
    if ($msg -ne '')
    {
      $lines += $msg.Split([System.Environment]::NewLine)
    }
  }
  process
  {
    if ($_)
    {
      if ($NoNewLine -and $lines.Count -gt 0)
      {
        $lines[0] = $lines[0] + [string]$_
      }
      else
      {
        $lines += ([string]$_).Split([System.Environment]::NewLine)
      }
    }
  }
  end
  {
    $lines | Log-Message -Level $Level -Kind $Kind -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -NonewLine:$NonewLine
  }
}

function Log-Error ([string]$msg, $Level='Info', $Kind='Error', $ForegroundColor='Red', $BackgroundColor='Black', [switch]$NonewLine)
{
  begin
  {
    $lines = @()
    if ($msg -ne '')
    {
      $lines += $msg.Split([System.Environment]::NewLine)
    }
  }
  process
  {
    if ($_)
    {
      if ($NoNewLine -and $lines.Count -gt 0)
      {
        $lines[0] = $lines[0] + [string]$_
      }
      else
      {
        $lines += ([string]$_).Split([System.Environment]::NewLine)
      }
    }
  }
  end
  {
    $lines | Log-Message -Level $Level -Kind $Kind -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -NonewLine:$NonewLine
  }
}

function Log-Exception($exception, $Level='Info')
{
}
